<?php
cmspad_import('database.DatabaseGroup');
class DatabaseEngine{

	/**
	 * 更新服务器组
	 *
	 * @var DatabaseGroup
	 */
	private $mConnection;

	/**
	 * 查询服务器组
	 *
	 * @var DatabaseGroup
	 */
	private $sConnection;
	
	public function __construct($type = 'mysql'){
		global $_DATABASE;
		
		$this->sConnection = new DatabaseGroup($type);
		$this->mConnection = new DatabaseGroup($type);
		
		if(isset($_DATABASE)){
			if(!isset($_DATABASE['ENCODING'])){
				$_DATABASE['ENCODING'] = 'utf8';
			}
			
			{ // Add slave databases
				if(isset($_DATABASE['SLAVE'])){
					foreach($_DATABASE['SLAVE'] as $database){
						if(isset($database['HOST']) && isset($database['NAME']) && isset($database['USER'])){
							$this->addSlave($database['HOST'], $database['NAME'], $database['USER'], $database['PSWD'], $database['PORT'], $_DATABASE['ENCODING']);
						}
					}
				}
				if(isset($_DATABASE['BOTH'])){
					foreach($_DATABASE['BOTH'] as $database){
						if(isset($database['HOST']) && isset($database['NAME']) && isset($database['USER'])){
							$this->addSlave($database['HOST'], $database['NAME'], $database['USER'], $database['PSWD'], $database['PORT'], $_DATABASE['ENCODING']);
						}
					}
				}
	
				// Support single database for simple configuration
				if(isset($_DATABASE['HOST']) && isset($_DATABASE['NAME']) && isset($_DATABASE['USER'])){
					$this->addSlave($_DATABASE['HOST'], $_DATABASE['NAME'], $_DATABASE['USER'], $_DATABASE['PSWD'], $_DATABASE['PORT'], $_DATABASE['ENCODING']);
				}
			}
	
			{ // Add master databases
				if(isset($_DATABASE['MASTER'])){
					foreach($_DATABASE['MASTER'] as $database){
						if(isset($database['HOST']) && isset($database['NAME']) && isset($database['USER'])){
							$this->addMaster($database['HOST'], $database['NAME'], $database['USER'], $database['PSWD'], $database['PORT'], $_DATABASE['ENCODING']);
						}
					}
				}
				if(isset($_DATABASE['BOTH'])){
					foreach($_DATABASE['BOTH'] as $database){
						if(isset($database['HOST']) && isset($database['NAME']) && isset($database['USER'])){
							$this->addMaster($database['HOST'], $database['NAME'], $database['USER'], $database['PSWD'], $database['PORT'], $_DATABASE['ENCODING']);
						}
					}
				}
	
				// Support single database for simple configuration
				if(isset($_DATABASE['HOST']) && isset($_DATABASE['NAME']) && isset($_DATABASE['USER'])){
					$this->addMaster($_DATABASE['HOST'], $_DATABASE['NAME'], $_DATABASE['USER'], $_DATABASE['PSWD'], $_DATABASE['PORT'], $_DATABASE['ENCODING']);
				}
			}
		}
	}

	/**
	 * 当这个类被注销时，断开数据库连接
	 */
	public function __destruct(){
		$this->mConnection->disconnect();
		$this->sConnection->disconnect();
	}

	/**
	 * 添加一个更新服务器
	 *
	 * @param string $host 数据库地址
	 * @param string $name 数据库名称
	 * @param string $username 登陆账号
	 * @param string $password 登陆密码
	 * @param int $port 连接的端口
	 * @param string $encoding 连接的字符编码
	 */
	public function addMaster($host, $name, $username, $password, $port = 3306, $encoding = 'utf8'){
		$this->mConnection->add($this->mConnection->getDatabaseCount(), $host, $name, $username, $password, $port, $encoding);
	}

	/**
	 * 添加一个查询服务器
	 *
	 * @param string $host 数据库地址
	 * @param string $name 数据库名称
	 * @param string $username 登陆账号
	 * @param string $password 登陆密码
	 * @param int $port 连接的端口
	 * @param string $encoding 连接的字符编码
	 */
	public function addSlave($host, $name, $username, $password, $port = 3306, $encoding = 'utf8'){
		$this->sConnection->add($this->sConnection->getDatabaseCount(), $host, $name, $username, $password, $port, $encoding);
	}

	/**
	 * @see DatabaseConnection#query()
	 */
	public function query($sql, $length = 0, $offset = 0, $period = 0){
		if($period > 0){
			// 使用缓存
			$fcclass = cmspad_import('cache.FunctionCache');
			$cache = new $fcclass(is_array($sql)?print_r($sql, true):$sql, array(&$this,'query'), array($sql, $length, $offset), $period);
			return $cache->value();
		}else{
			// 不使用缓存
			$this->_checkSlaveConnection();
			return $this->sConnection->query($sql, $length, $offset);
		}
	}
	
	/**
	 * @see DatabaseConnection#update
	 */
	public function update($sql, $length = 0, $offset = 0){
		$this->_checkMasterConnection();
		return $this->mConnection->update($sql, $length, $offset);
	}

	/**
	 * @see DatabaseConnection#prepare
	 * @return DatabaseStatement
	 */
	public function prepare($sql, $length = 0, $offset = 0){
		switch(strtoupper(substr($sql, 0, 5))){
			case 'SELEC':
			case 'SHOW ':
				$this->_checkSlaveConnection();
				return $this->sConnection->prepare($sql, $length, $offset);
			case 'INSER':
			case 'UPDAT':
			case 'DELET':
			case 'TRUNC':
				$this->_checkMasterConnection();
				return $this->mConnection->prepare($sql, $length, $offset);
			default:
				return false;
		}
	}

	/**
	 * @see DatabaseConnection#addBatch
	 */
	public function addBatch($sql){
		switch(strtoupper(substr($sql, 0, 5))){
			case 'SELEC':
			case 'SHOW ':
				$this->_checkSlaveConnection();
				$this->sConnection->addBatch($sql);
				break;
			case 'INSER':
			case 'UPDAT':
			case 'DELET':
			case 'TRUNC':
				$this->_checkMasterConnection();
				$this->mConnection->addBatch($sql);
				break;
		}
	}

	/**
	 * @see DatabaseConnection#executeBatch
	 */
	public function executeBatch(){
		if($this->mConnection->isConnected()){
			$r1 = $this->mConnection->executeBatch();
		}else{
			$r1 = false;
		}
		if($this->sConnection->isConnected()){
			$r2 = $this->sConnection->executeBatch();
		}else{
			$r2 = false;
		}
		if($r1 && $r2){
			return array_merge($r1, $r2);
		}else if($r1){
			return $r1;
		}else if($r2){
			return $r2;
		}else{
			return false;
		}
	}
	
	/**
	 * 检查更新数据库服务器连接，如果没有连接则连接数据库
	 */
	private function _checkMasterConnection(){
		if(!$this->mConnection->isConnected()){
			$this->mConnection->connect();
		}
	}
	
	/**
	 * 检查查询数据库服务器连接，如果没有连接则连接数据库
	 */
	private function _checkSlaveConnection(){
		if(!$this->sConnection->isConnected()){
			$this->sConnection->connect();
		}
	}
}
?>